﻿using System.Windows;
using System.Windows.Controls;
using appInterfaces;
using appDataContracts;
using System.Collections.Generic;

namespace appSupplier.UserControls
{
    /// <summary>
    /// Logique d'interaction pour UCProducts.xaml
    /// </summary>
    public partial class UCProducts : UserControl
    {

        public UCProducts()
        {
            InitializeComponent();
            dgProducts.ItemsSource = (List<SerializedProduct>)AppWindow.supplierService.getSupplierProducts(AppWindow.supplier.SUPPLIERID);
        }

        private void btAddProduct_Click(object sender, RoutedEventArgs e)
        {
            new CreateProductWindow(AppWindow.supplierService.getSupplierCatalogs(AppWindow.supplier.SUPPLIERID), this, false).ShowDialog();
        }

        private void btEdit_Click(object sender, RoutedEventArgs e)
        {
            new CreateProductWindow(AppWindow.supplierService.getSupplierCatalogs(AppWindow.supplier.SUPPLIERID), this, true).ShowDialog();

        }

        private void btDelete_Click(object sender, RoutedEventArgs e)
        {
            int id = ((SerializedProduct)dgProducts.SelectedItem).PRODUCTID;
            AppWindow.supplierService.deleteProduct(id);
            this.update();
        }

        public void update()
        {
            dgProducts.ItemsSource = (List<SerializedProduct>)AppWindow.supplierService.getSupplierProducts(AppWindow.supplier.SUPPLIERID);
        }

        private void dgProducts_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dgProducts.SelectedItems.Count > 0)
            {
                btEdit.Visibility = Visibility.Visible;
                btDelete.Visibility = Visibility.Visible;
            }
            else
            {
                btEdit.Visibility = Visibility.Hidden;
                btDelete.Visibility = Visibility.Hidden;
            }
        }

        //public void hideButtons()
        //{
        //    if (dgProducts.SelectedItems.Count == 0)
        //    {
        //        btEdit.Visibility = Visibility.Hidden;
        //        btDelete.Visibility = Visibility.Hidden;
        //    }
        //}

        private void dgProducts_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (dgProducts.SelectedItems.Count > 0)
            {
                btEdit_Click(sender, e);
            }
        }
    }
}
